/*
  in_unreal.dll - Winamp Plugin 
  
  Andrzej Oczkowicz [andREV] 
  andrew_lz@poczta.fm
  
  FileInfo Box dialog 
  
*/


#include <windows.h>
#include <commdlg.h>
#include <stdio.h>

char *file;
HWND howner;
HINSTANCE inst;

unsigned int fileSize(char *fn)
{
  FILE *f = fopen(fn, "rb");
  if (!fseek(f, 0, SEEK_END)){
    return ftell(f);
  }else{
    return -1;
  }      
}  

void save_music(char *file, HWND hwnd)
{
   OPENFILENAME ofn;
   char szFileName[MAX_PATH];
   char filter[MAX_PATH];
   
   char *music = (char *)extractMusic(file);   
   char *ext;
   int i;
   for (i = strlen(music); music[i] != '.'; i-- )
      ext = &music[i];
   ZeroMemory(filter, sizeof(filter));   
   strcpy(filter, "Music Files (*.");
   strcat(filter, ext);
   strcat(filter, ")\0*.");
   strcat(&filter[strlen(filter) + 2], ext);
   m(filter);
   ZeroMemory(&ofn, sizeof(ofn));
   strcpy(szFileName, music);
   ofn.lStructSize = sizeof(ofn);
   ofn.hwndOwner = hwnd;
   ofn.lpstrFilter = filter;
   ofn.lpstrFile = szFileName;
   ofn.lpstrDefExt = ext;
   ofn.nMaxFile = MAX_PATH;
   ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
               OFN_OVERWRITEPROMPT;
         
      if(GetSaveFileName(&ofn))
      {
        if (!CopyFile(music, ofn.lpstrFile, FALSE ))
          mbox("Error: CopyFileA(music, lpstrFile, FALSE)!");
        clearTmp(&music, TRUE);
      }
}      

LRESULT info_dlg(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{	 
  HWND ed;
  char ib[16];
  
	switch(msg)
	{	
		case WM_INITDIALOG:{
		   ed = FindWindowEx(hwnd, 0, "STATIC", "_icon");
		   HICON hIcon = LoadIcon(inst, "Unreal");
       SendMessage(ed, STM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
		   		   
		   ed = FindWindowEx(hwnd, 0, "EDIT", 0);
       SetWindowText(ed, file);
       
		   unsigned int ver = getVerByName(file);
       itoa(ver, ib, 12);
       ed = FindWindowEx(hwnd, 0, "STATIC", "_ver");
       SetWindowText(ed, ib); 
       
       ed = FindWindowEx(hwnd, 0, "STATIC", "_gname");
       SetWindowText(ed, (char *)getNameByType(getUFileTypeByVer(ver)) );
                           
		   ver = fileSize(file);
		   ver /= 1024;
		   itoa(ver, ib, 12);
		   strcat(ib, " kB"); 			     		   
       ed = FindWindowEx(hwnd, 0, "STATIC", "_size");
       SetWindowText(ed, ib);        
			break;
     }			
  	case WM_COMMAND:
  	   switch(wParam)
       {
           
         case 5:
           SendMessage(hwnd, WM_DESTROY, 0, 0);
           break; 
           
         case 6:
           show_musicinfo(file, howner);  
           break; 
           
         case 10:
          save_music(file, howner);  
          break;                                                     
  	   }    
			break;
    case WM_SYSCOMMAND :
            if (wParam == SC_CLOSE){ 
              SendMessage(hwnd, WM_DESTROY, 0, 0);	
            return 1;  
           }     
      break;    
         
		case WM_CLOSE:
		      SendMessage(hwnd, WM_DESTROY, 0, 0);
		      return 1;
			break;
           		
		case WM_DESTROY:
		      EndDialog(hwnd, 0);
		      return 1;
			break;
	}
	return 0;
}

void show_box(HINSTANCE instance, HWND owner, char *music_file)
{
  m("Show info dlg");
  file = music_file;
  howner = owner;
  inst = instance;
  DialogBoxParamA(instance, MAKEINTRESOURCE(130), owner, (DLGPROC)info_dlg, 0);   
}  

















